import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:im_base/public/components/layout_widgets.dart';
import 'package:im_base/private/app_ext.dart';
import 'package:im_base/public/ext/string_ext.dart';
import 'input/audio_overlay.dart';
import 'input/view.dart';
import 'items/chat_message.dart';
import 'logic.dart';
import 'state.dart';

class ChatWidget extends StatelessWidget {
  late final ChatLogic logic;
  late final ChatState state;
  final bool? showInput;
  final Widget? extendMenu;

  ChatWidget(
      {required String id,
      required bool isGroup,
      this.showInput,
      this.extendMenu,
      Function(String, String)? headerClick}) {
    logic = Get.put(ChatLogic(), tag: id);
    state = logic.state;
    this.state.id = id;
    this.state.isGroup = isGroup;
    logic.headerTap = headerClick;
  }

  @override
  Widget build(BuildContext context) {
    log('message------${logic == null}');
    return Stack(
      children: [
        GetBuilder<ChatLogic>(
            init: logic,
            tag: state.id,
            builder: (controller) {
              return LoadingWidget(
                status: state.status,
                child: Column(
                  children: [
                    Expanded(
                        child: InkWell(
                      onTap: () => logic.hideAllPop(),
                      splashColor: Colors.transparent,
                      highlightColor: Colors.transparent,
                      child: Container(
                        height: double.infinity,
                        width: double.infinity,
                        alignment: Alignment.topCenter,
                        child: ListView.builder(
                          shrinkWrap: true,
                          controller: state.chatController,
                          itemCount: state.chatHistory.length,
                          reverse: true,
                          itemBuilder: (context, index) {
                            if (index == state.chatHistory.length - 2) {
                              logic.loadMore();
                            }
                            return ChatMessage(
                              key: Key(state.chatHistory[index].msgID!),
                              message: state.chatHistory[index],
                            );
                          },
                        ),
                      ),
                    )),
                    Container(
                      child: extendMenu,
                    ),
                    (showInput ?? true)
                        ? InputWidget(id: state.id)
                        : Container()
                  ],
                ),
              );
            }),
        Positioned(
          child: AudioOverlay(state.id),
          top: Get.height * 0.5 - 160,
          left: Get.width * 0.5 - 80,
        ),
        GetBuilder<ChatLogic>(
            id: POP_MENU,
            init: logic,
            tag: state.id,
            builder: (controller) {
              if (state.showPopMenu) {
                return Positioned(
                  child: Container(
                    height: 40,
                    decoration: BoxDecoration(
                      color: Colors.black45,
                      borderRadius: BorderRadius.circular(8),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: getMenus(controller),
                    ),
                  ),
                  top: state.positionY,
                  left: state.positionX,
                );
              } else {
                return Container();
              }
            })
      ],
    );
  }
}

List<Widget> getMenus(ChatLogic controller) {
  var widgets = List<Widget>.empty(growable: true);
  if (controller.state.content!.isNotEmpty) {
    widgets.add(TextButton(
      onPressed: () {
        copyString(controller.state.content!);
        showToast("已复制到剪贴板");
        controller.hidePopMenu();
      },
      child: Text(
        "复制",
        style: TextStyle(color: Colors.white, fontSize: 14),
      ),
      style: ButtonStyle(
          padding: MaterialStateProperty.all(EdgeInsets.zero),
          minimumSize: MaterialStateProperty.all(Size(1, 1))),
    ));
  }
  widgets.add(TextButton(
      onPressed: () {
        controller.deleteMessage(controller.state.currendMsgID!);
      },
      child: Text(
        "删除",
        style: TextStyle(color: Colors.white, fontSize: 14),
      ),
      style: ButtonStyle(
          padding: MaterialStateProperty.all(EdgeInsets.zero),
          minimumSize: MaterialStateProperty.all(Size(1, 1)))));
  if (controller.state.canWithdraw && controller.state.isSelf!) {
    widgets.add(TextButton(
        onPressed: () {
          controller.withDrawMessage(controller.state.currendMsgID!);
        },
        child: Text(
          "撤回",
          style: TextStyle(color: Colors.white, fontSize: 14),
        ),
        style: ButtonStyle(
            padding: MaterialStateProperty.all(EdgeInsets.zero),
            minimumSize: MaterialStateProperty.all(Size(1, 1)))));
  }

  return widgets;
}
